//
//  DetailThemeViewController.m
//  WDFans
//
//  Created by Apple Inc on 4/27/12.
//  Copyright (c) 2012 AppleInc. All rights reserved.
//

#import "DetailThemeViewController.h"
#import "ZHDataService.h"
#import "ZHWebService.h"

#import "JSON.h"
#import "WDThemesDetail.h"
#import "DeliverThemeViewController.h"


static NSInteger nextPage = 1;

@interface DetailThemeViewController ()

@property (nonatomic, retain) NSMutableArray *themesDetail;
@property (nonatomic, assign) BOOL isNextPageExist;

@end

@implementation DetailThemeViewController

@synthesize themesDetail, isNextPageExist;
@synthesize tid,fid;
@synthesize nameTitle,display_url;
@synthesize wView;
@synthesize delegate;
@synthesize bar;
@synthesize lastPageBtn,nextPageBtn;
@synthesize pageLable;


-(IBAction)back
{
    [delegate backClick];
}

#pragma mark - Ryan methods
- (void)getAllThemesDetail:(NSInteger)page
{
    CompletionBlock completion = ^(NSData *data){
        NSString *s = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
        NSDictionary *responseDictionary = [s JSONValue];
        
        if(responseDictionary==nil)
        {
            NSLog(@"s=%@",s);
            NSLog(@"NO Data");
            
            return;
        }
        
        NSInteger tmpPage = nextPage;
        self.pageLable.text = [NSString stringWithFormat:@"%d/1",page];
        nextPage = [[responseDictionary objectForKey:@"page"] intValue];
        
        if (tmpPage==1&&nextPage==0) {
            NSLog(@"只有一页");
            self.lastPageBtn.enabled = NO;
            self.nextPageBtn.enabled = NO;
        }
        // 下一页&&不是最后一页
        else if (tmpPage<nextPage) {
            NSLog(@"下一页&&不是最后一页");
            
            if (tmpPage==1) {
                self.lastPageBtn.enabled = NO;
            }
            else
                self.lastPageBtn.enabled = YES;
            self.nextPageBtn.enabled = YES;
        }
        // 下一页&&是最后一页
        else if(tmpPage>nextPage&&nextPage==0) {
            NSLog(@"下一页&&是最后一页");
            self.lastPageBtn.enabled = YES;
            self.nextPageBtn.enabled = NO;
        }
        // 上一页&&是第一页
        else if(tmpPage>nextPage&&nextPage==1) {
            NSLog(@"上一页&&是第一页");
            self.lastPageBtn.enabled = NO;
            self.nextPageBtn.enabled = YES;
        }
        // 上一页&&不是第一页
        else
        {
            NSLog(@"上一页&&不是第一页");
            self.lastPageBtn.enabled = YES;
            self.nextPageBtn.enabled = YES;
        }
            
        

        
        self.display_url = [responseDictionary objectForKey:@"display_url"];

        NSDictionary *dict1 = [responseDictionary objectForKey:@"forum"];
            
        NSString* tmpStr = [dict1 objectForKey:@"name"];
        if ([tmpStr length]>0) {
            self.bar.title = tmpStr;
//                self.nameTitle = tmpStr;
//                self.title = tmpStr;
        }
            
        tmpStr = [dict1 objectForKey:@"display_url"];
        if ([tmpStr length]>0) {
            self.display_url = tmpStr;
        }
            
        NSDictionary *dict2 = [responseDictionary objectForKey:@"postlist"];
            
        
        if ([themesDetail count]>0) {
            [themesDetail removeAllObjects];
        }
        
        NSString* str = nil;
        
        for(NSString *sid in [dict2 allKeys])
        {
//            NSLog(@"sid=%@",sid);
            NSDictionary *d1 = [dict2 objectForKey:sid];
            WDThemesDetail* f = [[WDThemesDetail alloc] init];
            f.pid = [d1 objectForKey:@"pid"];
            f.fid = [d1 objectForKey:@"fid"];
            f.tid = [d1 objectForKey:@"tid"];
            f.first = [d1 objectForKey:@"first"];
            f.author = [d1 objectForKey:@"author"];
            f.authorid = [d1 objectForKey:@"authorid"];
            f.subject = [d1 objectForKey:@"subject"];
            f.dateline = [d1 objectForKey:@"dateline"];
            f.message = [d1 objectForKey:@"message"];
            f.avatar = [d1 objectForKey:@"avatar"];
            
            if ([str length]<1) {
                str = [NSString stringWithFormat:@"<font size=2 color=gray>作者：%@   发表于:%@</font><br/><br/>%@",f.author,f.dateline,f.message]; 
            }
            else
            {
                str = [NSString stringWithFormat:@"%@<br/><br/><hr><font size=2 color=gray>作者：%@   发表于:%@</font><br/><br/>%@",str,f.author,f.dateline,f.message]; 
            }
            
            
            
            [self.themesDetail addObject:f];
            
            [f release];
        }
        
        [self.wView loadHTMLString:str baseURL:nil];

    };
    ErrorBlock error = ^(NSString *error){};
    [[ZHWebService sharedService] getThemeDetailsWithSectionId:tid page:page onCompletion:completion onError:error];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    
    [pageLable release];
    [lastPageBtn release];
    [nextPageBtn release];
    [bar release];
    wView.delegate = nil;
    [wView release];
    [display_url release];
    [nameTitle release];
    [themesDetail release];
    
    [super dealloc];
}

-(void)viewWillDisappear:(BOOL)animated
{
    nextPage = 1;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.themesDetail = [NSMutableArray array];
    
    self.lastPageBtn.enabled = NO;
    self.nextPageBtn.enabled = NO;
    
//    self.pageLable.text = [NSString stringWithFormat:@"%d/1",nextPage];
    
    [self getAllThemesDetail:nextPage];
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    return navigationType != UIWebViewNavigationTypeLinkClicked;
}

-(IBAction)nextpageClick
{
    if (nextPage==0) {
        self.nextPageBtn.enabled = NO;
        return;
    }
    
    [self getAllThemesDetail:nextPage];
}

-(IBAction)lastPageClick
{
    if (nextPage<=2) {
        self.lastPageBtn.enabled = NO;
        return;
    }
    if (nextPage-2<1) {
        [self getAllThemesDetail:1];
    }
    else
    {   
        nextPage = nextPage-2;
        [self getAllThemesDetail:nextPage];
    }
}

-(IBAction)replyClick
{
    if(![ZHDataService isLogined])
    {
        UIAlertView *alertView  = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登陆" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
        [alertView release];
        
        return;
    }
    
    
    DeliverThemeViewController* vc = [[DeliverThemeViewController alloc]initWithNibName:@"DeliverThemeViewController"  bundle:nil];
    vc.viewType = kReplyType;
    vc.titleName = self.bar.title;
    vc.tid = tid;
    vc.fid = fid;
    [self presentModalViewController:vc animated:YES];

    [vc release];
    
    
    
}


@end
